package com.example.user_service.dao;

import com.example.user_service.dao.dt.FindUserById;
import com.example.user_service.vo.*;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import com.example.user_service.vo.Administrator;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@Mapper
public interface UserSelect {
    //gfc 管理员登录
    @Select("select account,pawd from administrator where account=#{account} and pawd = #{pawd} ")
    Administrator login_Administrator(Administrator administrator);

    //gfc 查询手机号是否被注册
    @Select("select count(1) from user_login where tel = #{tel}")
    int login_istel(String tel);

    //gfc 查询账号否被注册
    @Select("select count(1) from user_login where account = #{name}")
    int login_isname(String name);


    //gfc 通过手机查询userid
    @Select("select user_id from user_login where tel = #{tel}")
    Integer insert_select_userid(String tel);

    //gfc 登录
    @SelectProvider(type = FindUserById.class, method = "findUserById")
    UserVO is_login(@Param("name") String name,@Param("pawd") String pawd,@Param("tyle") String tyle);



    /* 条件查询管理员
     * @author Liu
     * @date 2021/12/14 23:01
     * @param null
     * @return
     * @throws
     * @since
     */
    @Select({"<script>",
            "SELECT a.account,a.pawd,a.state,a.createdatetime,r.role_name FROM administrator as a ,t_role as r,user_role as c where a.account = c.user_id and r.id = c.role_id",
            "<if test='createdatestr!=\"\" and enddatestr!=\"\" and account!=\"\"' >",
            "  createdatetime &gt;= '${createdatestr}' AND createdatetime &lt;= '${enddatestr}' and account like '%${account}%' ORDER BY createdatetime DESC",
            "</if>",
            "<if test='createdatestr!=\"\" and enddatestr==\"\" and account==\"\"' >",
            " createdatetime &gt;= '${createdatestr}'  ORDER BY createdatetime DESC",
            "</if>",
            "<if test='createdatestr!=\"\"  and account!=\"\" and enddatestr==\"\"' >",
            "  createdatetime &gt;= '${createdatestr}'  and account like '%${account}%' ORDER BY createdatetime DESC",
            "</if>",
            "<if test='createdatestr==\"\"  and account!=\"\" and enddatestr==\"\"' >",
            "   account like '%${account}%' ORDER BY createdatetime DESC",
            "</if>",
            "<if test='createdatestr!=\"\"  and account==\"\" and enddatestr!=\"\"' >",
            "   createdatetime &gt;= '${createdatestr}' AND createdatetime &lt;= '${enddatestr}' ORDER BY createdatetime DESC",
            "</if>",
            "</script>"
    })
    List<Administrator> AdministratorSelect(@Param("createdatestr") String createdatestr,@Param("enddatestr") String enddatestr,@Param("account") String account);

    /* 校验管理员账号是否存在
     * @author Liu
     * @date 2021/12/15 0:10
     * @param null
     * @return
     * @throws
     * @since
     */
    @Select("select * from administrator where account=#{account} ")
    Administrator check_Administrator(int account);


    /* 查询用户积分
     * @author Liu
     * @date 2021/12/17 11:23
     * @param null
     * @return
     * @throws
     * @since
     */
    @Select("select * from user_member where user_id = ${userid}")
    User_Member select_member_existing_integral(int userid);

    /* 查询我的足迹
     * @author Liu
     * @date 2021/12/17 11:49
     * @param null
     * @return
     * @throws
     * @since
     */
    @Select("select * from my_tracks where user_id = ${userid} ORDER BY tracks_create desc")
    List<MyTracks> select_my_tracks(int userid);

    /* 查询用户收藏
     * @author Liu
     * @date 2021/12/19 23:01
     * @param null
     * @return
     * @throws
     * @since
     */
    @Select("select * from collect_goods where user_id = ${userid} ORDER BY Collection_create desc")
    List<CollectGoods> select_collect_goods(int userid);

    /* 查询用户是否收藏
     * @author Liu
     * @date 2021/12/20 8:34
     * @param null
     * @return
     * @throws
     * @since
     */
    @Select("select * from collect_goods where user_id = ${user_id} and Commodity_id = ${footprint_id} ")
    List<CollectGoods> select_collect_goods1(@Param("user_id") int user_id,
                                             @Param("footprint_id") int footprint_id);




    /* 查询个人中心信息
     * @author Liu
     * @date 2021/12/21 17:31
     * @param null
     * @return
     * @throws
     * @since
     */
    @Select("select type.member_type_name,x.user_name,x.member_existing_integral,x.member_overdue,x.user_img from member_type as type ,(select u.user_name,u.user_img,me.member_grade_id,me.member_overdue,me.member_existing_integral from user_data as u,user_member as me where u.user_id = me.user_id and u.user_id = ${user_id}) as x where x.member_grade_id = type.member_type_id")
    List<User_welcome> select_user_welcome(int user_id);

    /* 查询会员福利
     * @author Liu
     * @date 2021/12/21 19:56
     * @param null
     * @return
     * @throws
     * @since
     */

    @Select("select a.member_welfare_discount from member_welfare as a, (select member_welfare_id from member_type as mt,(select um.member_grade_id from user_member as um where user_id = ${userid})as um where mt.member_type_id = um.member_grade_id)as x where a.member_welfare_id = x.member_welfare_id")
    User_Member select_user_member_zeko(@Param("userid") int userid);



    /* 富诚查询用户足迹表的商品分类id
     * @author Liu
     * @date 2021/12/21 22:30
     * @param null
     * @return
     * @throws
     * @since
     */
    @Select("select goods_type FROM my_tracks where user_id = #{userid} limit 0,20")
    List<Integer> getmy_tracks(@Param("userid") int userid);


    /* 校验用户足迹表是否有次足迹
     * @author Liu
     * @date 2021/12/21 22:37
     * @param null
     * @return
     * @throws
     * @since
     */
    @Select("select * FROM my_tracks where user_id = ${user_id} and footprint_id = ${footprint_id} and goods_type = ${goods_type}")
    MyTracks check_my_tracks(int user_id, int footprint_id, int goods_type);

    @Select("select * from miaosha where goodsid = #{goodsid} and miao_time2 > #{time}")
    Miaosha getmiaosha(int goodsid,String time);




}
